<?php

class Admin_AclResourceController extends Custom_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
	}

    public function indexAction()
    {
		$config = Zend_Registry::get('config');
        $resource = new User_Model_DbTable_AclResource();
		$page = $this->getRequest()->getParam('page');
		$resource->setPaginator($this->Config->params->recordsPerPage,$page);
		$this->setSort($resource);
		$this->view->resources = $resource->getAll($this->searchKey);
		$this->view->paginator = $resource->getPaginator();

		$this->view->page = $page;

		#set submenu
		$this->_setTab(0);
    }

    public function createAction()
    {
        $form = new Admin_Form_AddAclResource();
        $this->view->form = $form;

		if($this->getRequest()->isPost())
        {
            $formData = $this->getRequest()->getPost();

            if($form->isValid($formData))
            {
				$aclResource = new User_Model_DbTable_AclResource();
				$data = $form->getValues();
				
				$res = $aclResource->insert($data);
				
				if($res)
				{
					$this->addMessage('Acl resource added successfully');
					$this->Redirector('add','aclresource','admin');
				}
				else
					$this->addMessage('Acl resource not added successfully','error');
			}
		}

		#set submenu
		$this->_setTab(1);
    }

    public function editAction()
    {
		$id = $this->_getParam('id');

		if($id)
		{
			$objAclResource = new User_Model_DbTable_AclResource();
			$resource = $objAclResource->getOne($id);

			$form = new Admin_Form_AddAclResource($id);
			$form->populate($resource);
			$this->view->form = $form;

			if($this->getRequest()->isPost())
			{
				$formData = $this->getRequest()->getPost();

				if($form->isValid($formData) && $id)
				{
					$data = $form->getValues();
					$res = $objAclResource->updateById($data, $id);

					if($res)
						$this->addMessage('Acl resource updated successfully');
					else
						$this->addMessage('Acl resource not updated successfully','error');


					if($this->_getParam('page'))
						$params = array('id'=>$id, 'page'=>$this->_getParam('page'));
					else
						$params = array('id'=>$id);

					$this->Redirector('index','aclresource','admin',$params);
				}

			}
		}
		else
		{
			$this->addMessage('Acl resource not found');
		}

		#set submenu
		$this->_setTab(3);
    }

    public function deleteAction()
    {
        $id = $this->_getParam('id');

		if($id)
		{
			$objAclResource = new User_Model_DbTable_AclResource();
			$res = $objAclResource->deleteById($id);

			if($res)
				$this->addMessage('Acl resource delete successfully');
			else
				$this->addMessage('Acl resource not delete successfully','error');

			if($this->_getParam('page'))
				$params = array('page'=>$this->_getParam('page'));
			else
				$params = array();

			$this->Redirector('index','aclresource','admin',$params);
		}
    }

	public function autoAction()
	{
		$resources = $this->_helper->AssetsList->getList();
		$objResource = new User_Model_DbTable_AclResource();
		$foundResource = null;
		$count = 0;

		foreach($resources as $key => $resource)
		{
			$arrResources = array_keys($resource);

			foreach($arrResources as  $value)
			{
				$data = '';
				$res = explode("_",$value);
				$data['resource'] = $value;
				$data['description'] = ucfirst($res[1])." Controller - ".ucfirst($res['0'])." Module";
				$res = $objResource->insert($data);

				if($res)
				{
					$foundResource .= $data['description']."<br/>";
					$count++;
				}
			}
		}

		if($count)
		{
			if($count > 1)
				$this->addMessage(sprintf(__('Total %s new resources found'),$count));
			else
				$this->addMessage(__('Total 1 new resource found'));
			
			$this->view->foundResource = $foundResource;
		}
		else
		{
			$this->addMessage(__('No new resource found'),'error');
			$this->view->foundResource = __('No new resource found');
		}

		#set submenu
		$this->_setTab(2);
	}

	private function _setTab($activeIndx)
	{
		$subItems = array(__('ACL Resources'),__('Create Resource'),__('Auto Resource'),__('Edit Resource'));
		$subItemLinks = array('admin/aclresource','admin/aclresource/create','admin/aclresource/auto');

		$menues = array('0','1','2');
		if(!in_array($activeIndx, $menues)) $menues[] = $activeIndx;

		$subMenues = array('items' => $subItems, 'links'=> $subItemLinks,
						   'enableMenues' => $menues, 'activeIndx' => $activeIndx);

		$this->view->subMenues = $subMenues;
	}
}







